Skip to content

Conversation

@mandesero
Copy link
Contributor

@mandesero mandesero commented Nov 25, 2025

This patch adds unified diff output for t.assert_equals() failures when the test suite is run with the --diff option, using a vendored Lua implementation of google/diff-match-patch (luatest/vendor/diff_match_patch.lua taken from 1).

Closes #412


Here is the output for the example from the issue:

code
local t = require('luatest')

local g = t.group()

g.test_table_assertion = function()
    local a = {
      a = {
        ["a"] = 1,
        ["b"] = 2,
        ["c"] = 3,
      },
    }

    local b = {
      a = {
        ["a"] = 1,
        ["b"] = 5,
        ["c"] = 8,
      },
    }
    t.assert_equals(a, b, 'compare tables')
end

g.test_string_assertion = function()
    local a = [[
aaaaa
bbbbb
ccccc
]]
    local b = [[
aaaaa
ddddd
ccccc
]]

    t.assert_equals(a, b, 'compare multiline strings')
end

g.test_msgpack_assertion = function()
    local msgpack = require('msgpack')
    local a = {1, 2, 3}
    local b = {1, 4, 3}

    t.assert_equals(msgpack.encode(a), msgpack.encode(b), 'compare encoded msgpack')
end
Failed tests:
-------------

1) tmp.test_table_assertion
/home/ubuntu/luatest/test/tmp_test.lua:21: compare tables
expected: {a = {a = 1, b = 5, c = 8}}
actual: {a = {a = 1, b = 2, c = 3}}
diff:
@@ -1,32 +1,32 @@
 ---
 a:
-  b: 5
  a: 1
  c: 8
+  b: 2
  a: 1
  c: 3
 ...

2) tmp.test_string_assertion
/home/ubuntu/luatest/test/tmp_test.lua:36: compare multiline strings
expected:
"aaaaa\
ddddd\
ccccc\
"
actual:
"aaaaa\
bbbbb\
ccccc\
"
diff:
@@ -1,34 +1,34 @@
 --- |
  aaaaa
-  ddddd
+  bbbbb
   ccccc
 ...

3) tmp.test_msgpack_assertion
/home/ubuntu/luatest/test/tmp_test.lua:44: compare encoded msgpack
expected: "�\1\4\3"
actual: "�\1\2\3"
diff:
@@ -1,20 +1,20 @@
 ---
- 1
-- 4
+- 2
 - 3
 ...

Footnotes

  1. https://github.com/google/diff-match-patch/blob/master/lua/diff_match_patch.lua

Copy link
Member

@ligurio ligurio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Max, thanks for the patch!
Please take a look on my comments.

@mandesero mandesero force-pushed the gh-412-print-diff branch 2 times, most recently from 1d91b35 to 26138ab Compare November 27, 2025 23:20
@mandesero mandesero requested a review from ligurio November 27, 2025 23:23
@mandesero mandesero requested a review from ligurio December 1, 2025 07:52
@mandesero mandesero force-pushed the gh-412-print-diff branch 3 times, most recently from edcc8eb to 6a22390 Compare December 3, 2025 15:09
Copy link
Member

@ligurio ligurio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

This patch adds unified diff output for `t.assert_equals()` failures,
using a vendored Lua implementation of google/diff-match-patch
(`luatest/vendor/diff_match_patch.lua` taken from [^1]).

Closes tarantool#412

[^1]: https://github.com/google/diff-match-patch/blob/master/lua/diff_match_patch.lua
@mandesero mandesero assigned locker and unassigned mandesero Dec 10, 2025
@mandesero mandesero requested a review from locker December 10, 2025 09:40
local used_line_diff = true

if diffs == nil then
diffs = diff_match_patch.diff_main(expected_text, actual_text)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there's no test for this fallback mode - if I remove this code, the tests still pass.

Why can't we compute a line-based diff in all cases? Is there any point in char-based diff? Does it look good?

same: nested-shared
s: qux
- n: 10
dec: '1.23'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value of dec is different (1.23 vs 3.21). Why isn't it reported here? BTW do you take into account that the order of keys in a YaML document is undefined? Need to sort it?

@locker locker assigned mandesero and unassigned locker Dec 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Print a diff for compared data

3 participants